package cvi.com.reader.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.orhanobut.hawk.Hawk;

import butterknife.BindView;
import butterknife.ButterKnife;
import cvi.com.reader.R;
import cvi.com.reader.base.BaseActivity;
import cvi.com.reader.bean.PartyMemberInfoBean;
import cvi.com.reader.bean.constant;
import cvi.com.reader.network.DefaultObserver;
import cvi.com.reader.network.NetWorkManager;
import cvi.com.reader.tool.UtilsOnclickListen;
import de.hdodenhof.circleimageview.CircleImageView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by 梅涛 on 2020/11/19.
 * 党员信息页面
 */

public class PartyMemberInfoActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.secret_insurance_answer)
    TextView secretInsuranceAnswer;
    @BindView(R.id.right_ll1)
    LinearLayout rightLl1;
    @BindView(R.id.poto)
    CircleImageView poto;
    @BindView(R.id.right_ll2)
    LinearLayout rightLl2;
    @BindView(R.id.right_ll3)
    LinearLayout rightLl3;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.right_ll4)
    LinearLayout rightLl4;
    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.tittle)
    TextView tittle;
    @BindView(R.id.right_icon)
    LinearLayout rightIcon;
    @BindView(R.id.party_branch)
    TextView partyBranch;
    private MyReceiver myReceiver;
    private String aconuntStr = "";
    private String is_party_member = "0";

    @Override
    public void onClick(View view) {
        Bundle bundle = null;
        switch (view.getId()) {
            case R.id.back:
                //登录
                finish();
                break;
            case R.id.right_ll1:
            case R.id.poto:
                if (UtilsOnclickListen.isFastClick()) {
                    jump(SelectPictureActivity.class);
                }
                break;
            case R.id.right_ll2:
            case R.id.name:
                if (UtilsOnclickListen.isFastClick()) {
                    bundle = new Bundle();
                    if (name.getText().toString().trim().isEmpty()) {
                        bundle.putString("name", "");
                    } else {
                        bundle.putString("name", name.getText().toString().trim());
                    }

                    jump(UpdataNameActivity.class, bundle);
                }
                break;
            case R.id.right_ll3:
            case R.id.party_branch:
                if (UtilsOnclickListen.isFastClick()) {
                    bundle = new Bundle();
                    if (partyBranch.getText().toString().trim().isEmpty()) {
                        bundle.putString("part", "");
                    } else {
                        bundle.putString("part", partyBranch.getText().toString().trim());
                    }
                    bundle.putString("is_party_member", is_party_member);
                    jump(UpdataPartBranchActivity.class, bundle);
                }
                break;
            case R.id.right_ll4:
                if (UtilsOnclickListen.isFastClick()) {
                    bundle = new Bundle();
                    bundle.putString("acount", aconuntStr);
                    jump(UpdataPassWordActivity.class, bundle);
                }
                break;
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_party_member_info;
    }

    @Override
    protected void initView() {
        back.setOnClickListener(this);
        name.setOnClickListener(this);
        poto.setOnClickListener(this);
        partyBranch.setOnClickListener(this);
        rightLl1.setOnClickListener(this);
        rightLl2.setOnClickListener(this);
        rightLl3.setOnClickListener(this);
        rightLl4.setOnClickListener(this);
        tittle.setText("用户信息");
    }

    @Override
    protected void initData() {
        //党支部接口
        NetWorkManager.getInstance().getNetWorkApiService().getPartyMemberInfo().compose(this.<JSONObject>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<JSONObject>(PartyMemberInfoActivity.this) {
                    @Override
                    protected void onSuccess(JSONObject response) {
                        if (response != null) {
                            setParty(response);
                        }

                    }
                });

    }

    private void setParty(JSONObject response) {
        PartyMemberInfoBean partyMemberInfoBean = response.toJavaObject(PartyMemberInfoBean.class);
        name.setText(partyMemberInfoBean.getData().getUser_info().getName());
        partyBranch.setText(partyMemberInfoBean.getData().getUser_info().getParty_branch());
        aconuntStr = partyMemberInfoBean.getData().getUser_info().getAccount();
        is_party_member = partyMemberInfoBean.getData().getUser_info().getIs_party_member() + "";
        String strUrl = null;
       /* if (Hawk.contains("ip")) {
            strUrl = Hawk.get("ip");
        }
        if (strUrl != null) {
            ImageLoader.getInstance().displayImage("http://" + strUrl + partyMemberInfoBean.getData().getUser_info().getAvatar(), poto);
        }*/
        ImageLoader.getInstance().displayImage("http://" + constant.URL + partyMemberInfoBean.getData().getUser_info().getAvatar(), poto);

    }

    @Override
    protected void extrasDoSome(Bundle extras) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(constant.UPDATA_PARTY_MENBER_INFO_MYRECEIVER);
        myReceiver = new MyReceiver();
        //注册广播
        registerReceiver(myReceiver, intentFilter);
    }

    class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            initData();
        }
    }
}
